package main

import (
	"fmt"
	"io"
	"os"

	"gitee.com/czy233/go-ceph/rados"
)

func fake_main() {
	conn, err := rados.NewConn()
	if err != nil {
		fmt.Println("error when invoke a new connection:", err)
		return
	}

	err = conn.ReadDefaultConfigFile()
	if err != nil {
		fmt.Println("error when read default config file:", err)
		return
	}

	err = conn.Connect()
	if err != nil {
		fmt.Println("error when connect:", err)
		return
	}

	fmt.Println("connect ceph cluster ok!")

	// stats, err := conn.GetClusterStats()
	// if err != nil {
	// 	fmt.Println("error when get cluster stats:", err)
	// 	return
	// }
	// fmt.Println("ceph cluster stats:", stats)

	// pools, err := conn.ListPools()
	// if err != nil {
	// 	fmt.Println("error when list pools:", err)
	// 	return
	// }
	// for i := 0; i < len(pools); i++ {
	// 	fmt.Println(pools[i])
	// }

	ioctx, err := conn.OpenIOContext("testbench")
	if err != nil {
		fmt.Println("error when open IO context:", err)
		return
	}

	// get obj content
	oid := "benchmark_data_node1_1279869_object15434"
	var data []byte
	data = make([]byte, 4194305)
	ret, err := ioctx.Read(oid, data, 0)
	if err != nil {
		fmt.Println("error when read obj:", err)
		return
	}

	// write obj content to file
	f, err := os.OpenFile("/home/mcloud/go/src/ceph-go/"+oid, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
	if err != nil {
		fmt.Println("error when open file:", err)
		return
	}
	n, err := f.Write(data[:ret])
	if err == nil && n < ret {
		err = io.ErrShortWrite
		fmt.Println("error when write obj to file:", err)
	}
	f.Close()

	conn.Shutdown()
}
